/*
    Tray Timer, a system tray stopwatch/timer
    Copyright (C) 2009  Pratik J Bhatt
	http://traytimer.codeplex.com/

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version. Refer to LICENSE.txt for the full
	text of the license.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "Alarm.h"

Alarm::Alarm(void)
{
	intervel = 0;
	precision = IDC_MINUTES;
	on = false;
}

Alarm::Alarm(bool on, UINT intervel, UINT precision)
{
	this->on = on;
	this->intervel = intervel;
	this->precision = precision;
}

Alarm::~Alarm(void)
{
}


bool Alarm::ringAlarm(long int currentTime, UINT timerPrecision)
{
	long int remainder = 0;

	if (timerPrecision != precision)					//If the timer precision and alarm precision are not same, 
	{													//We have to convert the current time to correct precision.
		switch (timerPrecision)
		{
		case IDC_SECONDS:
			switch (precision)							//Only check convert for IDC_MIN and IDC_HR
			{
			case IDC_MINUTES:
				remainder = currentTime % 60;
				currentTime = currentTime / 60;			//Convert time from Seconds to the nearest Minute
				break;
			case IDC_HOURS:
				remainder = currentTime % 3600;
				currentTime = currentTime / 3600;		//Convert time from seconds to the nearest Hour
				break;
			};
			break;
		case IDC_MINUTES:								//We only need to check for Hours
			if (precision == IDC_HOURS)		
			{
				remainder = currentTime % 60;
				currentTime = currentTime / 60;			//Convert time from minutes to the nearest Hour
			}
			break;
		};												//We dont have to check for hours, because there can only be one alarm precision with hours.
	}

	if(remainder > 0)									//Make sure currentTime is exactly divsible by 60 or 3600. This will prevent the alarm from going
		return false;									//off at every timer event after the first alarm or whenever currentTime turns out to be zero.
	
	if ((currentTime % intervel) == 0)					//If there is no remainder, it means the current time is perfectly divisible by intervel.
		return true;									//This means the alarm intervel has elapsed and we should post the alarm.

	return false;
}

bool Alarm::isOn()
{
	return on;
}